// components/detail/CommentItem.js
const computedBehavior = require("miniprogram-computed").behavior;
const db = wx.cloud.database();
const collection = "comments";
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [computedBehavior],
  options: {
    "styleIsolation": 'apply-shared',
  },
  properties: {
    comment: {
      type: Object,
      value: {}
    },
    user: {
      type: Object,
      value: {}
    }
  },
  computed: {
    time(data) {
      let time = data.comment.time;
      let temp = new Date(time);
      let hours = temp.getHours();
      hours = hours < 10 ? "0" + hours : hours;
      let minutes = temp.getMinutes();
      minutes = minutes < 10 ? "0" + minutes : minutes;
      let seconds = temp.getSeconds();
      seconds = seconds < 10 ? "0" + seconds : seconds;
      time = `${temp.getFullYear()}-${temp.getMonth()+1}-${temp.getDate()} ${hours}:${minutes}:${seconds}`;
      return time;
    }
  },
  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    async deleteItem() {
      // console.log(this.data.comment);
      wx.showLoading({
        title: '正在删除'
      });
      let res = await db.collection(collection).where({
        id: app.globalData.bookInfo.id,
      }).get();
      let comments = res.data[0].comments;
      comments = comments.filter(item => item.commentId != this.data.comment.commentId);
      console.log(comments);
      wx.cloud.callFunction({
        name:"updateComments",
        data:{
          comments,
          id:app.globalData.bookInfo.id
        },
      }).then(res=>{
        wx.hideLoading();
        let list = this.data.comment.imgList.map(el => {
          return el.url;
        });
        this.triggerEvent("myevent",comments);
        wx.cloud.deleteFile({
          fileList: [...list],
          success(res) {
            console.log(res, '删除文件')
          },
          fail(err) {
            console.log(err)
          }
        });
        wx.showToast({
          title: '删除成功',
        });
      })
    }
  },
  lifetimes: {
    ready() {
      // console.log(this.data.comment, "comment");
    }
  }
})